package cn.lfzj.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.lfzj.model.Order;

public interface OrderDao {

	/**
	 * 订单查询(多条件)
	 */
	@Results({ @Result(property = "user", column = "user_id", one = @One(select = "cn.lfzj.dao.UserDao.selById")) })
	@Select("<script>" 
				+ "SELECT * FROM orders " 
					+ "<where>" 
						+ "<if test=\"orderId!=null and orderId!='' \">"
							+ "AND order_id=#{orderId}" 
						+ "</if>" 
						+ "<if test=\"startSta!=null and startSta!='' \">"
							+ "AND start_station=#{startSta}" 
						+ "</if>" 
						+ "<if test=\"endSta!=null and endSta!='' \">"
							+ "AND end_station=#{endSta}" 
						+ "</if>" + "<if test=\"data!=null and data!='' \">"
							+ "AND start_data like concat(#{data},'%')" 
						+ "</if>" 
						+ "<if test=\"orderState!=null and orderState!='' \">"
							+ "AND order_state=#{orderState}" 
						+ "</if>" 
						+ "<if test=\"userId!=0 \">" 
							+ "AND user_id=#{userId}" 
						+ "</if>"
					+ "</where>" 
				+ "order by create_time desc" 
			+ "</script>")
	public List<Order> selAllOrder(@Param("orderId") String orderId, @Param("userId") int userId,
			@Param("data") String data, @Param("startSta") String startSta, @Param("endSta") String endSta,
			@Param("orderState") String orderState);

	/**
	 * 创建订单
	 */
	@Insert("INSERT INTO orders "
			+ "VALUES(#{orderId},#{user.userId},now(),#{startStation},#{endStation},#{startData},#{amount},#{orderState})")
	public int addOrder(Order order);

	/**
	 * 修改订单状态
	 */
	@Update("UPDATE orders SET order_state=#{orderState},amount=#{amount} where order_id=#{orderId}")
	public int updateState(@Param("orderState") String orderState, @Param("amount") double amount,
			@Param("orderId") String orderId);

	/**
	 * 根据用户ID查订单
	 */
	@Select("SELECT * FROM orders WHERE user_id=#{userId} order by create_time desc")
	public List<Order> selByUserId(int userId);

	/**
	 * 删除订单
	 */
	@Delete("DELETE FROM orders WHERE order_id=#{orderId}")
	public int delOrder(String orderId);
}
